#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <string.h>
#include <stdlib.h>

#define BUFFSIZE 128
int main (void)
{
	int fd[2];
	int ret = 0;
	char *msg = "happy every day!\n";
	char buf[BUFFSIZE] = {};
	ret = pipe (fd);
	if (-1 == ret) {
		perror ("pipe()");
		return -1;
	}
	pid_t pid = fork ();
	if (-1 == pid) {
		perror ("fork()");
		exit(1);
	}
	if (0 == pid) {
		//子进程
		close (fd[0]);
		write (fd[1], msg, strlen(msg));
		close (fd[1]);
		exit (0);
	} else {
		//父进程
		close (fd[1]);
		int cnt = read (fd[0], buf, BUFFSIZE);
		write (1, buf, cnt);
		close (fd[0]);
		wait (NULL);
	}
	return 0;
}
